package com.hxnidc.horizontaltest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView, arecyclerView;
    private CommonAdapter<MiningBean.DataBean> commonAdapter;
    private CommonAdapter<WorkerAlarmBean> WorkerAdapter;
    private List<MiningBean.DataBean> list = new ArrayList<>();
    private List<WorkerAlarmBean> workerAlarmBeanList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        getData();

        getWorket();

        recyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
        arecyclerView = (RecyclerView) findViewById(R.id.arecyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, true);
        recyclerView.setLayoutManager(linearLayoutManager);


        commonAdapter = new CommonAdapter<MiningBean.DataBean>(this, R.layout.item_rv_layout, list) {
            @Override
            protected void convert(ViewHolder holder, MiningBean.DataBean s, int position) {
                if (s == null) return;
                holder.setText(R.id.text1, "名称");
                holder.setText(R.id.text2, "ETH");
                holder.setText(R.id.text3,s.getWorker().toUpperCase());
                if (s.getReportedHashrate() == 0) {
                    holder.setText(R.id.text4, "");
                } else {
                    holder.setText(R.id.text4, StringUtils.division(s.getReportedHashrate(), 1000000) + "MH/s");
                }
                if (s.getCurrentHashrate() == 0) {
                    holder.setText(R.id.text5, "");
                } else {
                    holder.setText(R.id.text5, StringUtils.format(s.getCurrentHashrate() / 1000000) + "MH/s");
                }
                if (s.getAverageHashrate() == 0) {
                    holder.setText(R.id.text6, "");
                } else {
                    holder.setText(R.id.text6, StringUtils.format(s.getAverageHashrate() / 1000000) + "MH/s");

                }
                if ((s.getReportedHashrate() / 1000000) > 150) {
                    holder.setText(R.id.text7, "HROL");
                } else {
                    holder.setText(R.id.text7, "OFF");
                }
                holder.setText(R.id.text8, millis2String(s.getTime()));
            }
        };
        recyclerView.setAdapter(commonAdapter);

        arecyclerView.setLayoutManager(linearLayoutManager1);
        WorkerAdapter = new CommonAdapter<WorkerAlarmBean>(this, R.layout.item_rv1_left_layout, workerAlarmBeanList) {
            @Override
            protected void convert(ViewHolder holder, WorkerAlarmBean s, int position) {
                if (s == null) return;
                holder.setText(R.id.textview1, s.getName());
                holder.setText(R.id.textview2, s.getType());
                holder.setText(R.id.textview3, s.getNameId());
                holder.setText(R.id.textview4, s.getStatus());
            }
        };
        arecyclerView.setAdapter(WorkerAdapter);
    }

    private void getWorket() {
        for (int i = 0; i < 100; i++) {
            WorkerAlarmBean workerAlarmBean = new WorkerAlarmBean();
            workerAlarmBean.setName("AA");
            workerAlarmBean.setType("ETH");
            workerAlarmBean.setNameId("J2-4");
            workerAlarmBean.setStatus("OFF");
            workerAlarmBeanList.add(workerAlarmBean);
        }
    }

    private void getData() {

        OkHttpUtils.get().url("https://api.ethermine.org/miner/c0c00cd1d93fb7e3f7d34bdd9007e360ef6c2556/workers").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(MainActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onResponse(String response, int id) {

                if (TextUtils.isEmpty(response))
                    return;
                Gson gson = new Gson();
                MiningBean miningBean = gson.fromJson(response, MiningBean.class);
                list.addAll(miningBean.getData());
                commonAdapter.notifyDataSetChanged();
            }
        });

    }

    public String stampToDate(long s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(s);
        res = simpleDateFormat.format(date);
        return res;
    }

    public String millis2String(long millis) {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(millis));
    }


}
